<?php
namespace app\home\Controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Cookie;
// use app\index\model\index;
class Cart extends Controller
{
	// 购物车
    public function submit()
    {
        $u_id = Cookie::get('u_id');
        if (!empty($u_id)) {
            // 用户购物车商品
            $sql = "select * from man_shopping_cart where u_id=$u_id and g_statc=0";
            $cartData = Db::query($sql);

            foreach ($cartData as $key => $value) {
                // 猜你喜欢
                $sql = "select * from man_goods where g_id='$value[g_id]' order by g_addtime desc limit 1";
                $goodsDataFx = Db::query($sql);
                $goodsDataFx1[] = $goodsDataFx[0];
            }

            $typeData = $this->alltype();
            return view('submit',[
                'typeData'=>$typeData,
                'cartData'=>$cartData,
                'goodsDataFx'=>$goodsDataFx1
                ]
            );
        }else{
            return redirect("index/index");
        }
    }
    // 加减修改数量
    public function submitNum()
    {
        $data = input('post.');
        if(!empty($data)){
            $u_id = Cookie::get('u_id');
            if(!empty($u_id)){
                $sql = "update man_shopping_cart set s_num='$data[num]',g_price='$data[g_price]' where g_id='$data[g_id]' and s_id='$data[s_id]' and u_id=$u_id";
                $res = Db::execute($sql);
                echo "1";
            }else{
                echo "2";
            }
        }
    }
    // 去结算
    public function submitStatc()
    {
        $data = input("post.");
        
        if (!empty($data)) {
            $u_id = Cookie::get('u_id');
            if(!empty($u_id)){
                $s_id = substr($data['s_id'],1);
                $g_statc = substr($data['g_statc'],1);
                $sql = "select * from man_shopping_cart where s_id in($s_id) and u_id=$u_id";
                $cartData = Db::query($sql);
                $cartData = json_encode($cartData,JSON_UNESCAPED_UNICODE);
                file_put_contents('cartData.txt',$cartData);
                echo "1";
            }else{
                echo "2";
            }
        }
    }

    // 确认订单信息
    public function submitpath()
    {
        $u_id = Cookie::get('u_id');
        if (!empty($u_id)) {
            // 订单商品
            $data = file_get_contents('cartData.txt');
            $cartData = json_decode($data,true);
            // 收货地址
            $sql = "select * from man_address where u_id=1";
            $addressData = Db::query($sql);

            // 省市级联动
            if($this->request->isAjax()){
                $area_id = input("get.area_id");
                $sql = "select * from man_area where area_parent_id=$area_id";
                $areaData = Db::query($sql);
                $return = array('areaData'=>$areaData);
                echo json_encode($return);
                die;
            }
            $sql = "select * from man_area where area_parent_id=0 and area_name='北京'";
            $areaData = Db::query($sql);
            
            $typeData = $this->alltype();
            return view('submitpath',[
                'typeData'=>$typeData,
                'areaData'=>$areaData,
                'cartData'=>$cartData,
                'addressData'=>$addressData
                ]
            );
        }else{
            return redirect("index/index");
        }
    }
    // 订单入库
    public function orderAdd()
    {
        $u_id = Cookie::get('u_id');
        if (!empty($u_id)) {
            $data = input("post.");
            if(!empty($data)){

                // 收货地址
                $sql = "select * from man_address where u_id=1 and ad_is_default=1";
                $addressInfo = Db::query($sql);
                $addressInfo = $addressInfo[0];

                // 购物订单商品
                $data1 = file_get_contents('cartData.txt');
                $cartData = json_decode($data1,true);
                if(!empty($cartData)){
                    $sql = "insert into man_order values";
                    foreach ($cartData as $key => $value) {
                        // 订单编号
                        $o_sn = time().rand(9999,99999);
                        $time = time();
                        $o_remark = isset($data['o_remark'])?$data['o_remark']:'';
                        $sql.="(null,'$o_sn',0,$u_id,'$value[g_id]','$value[g_name]','$value[g_img]','$value[gui_name]','$data[sum_price]',0,$time,1,'$addressInfo[ad_name]','$addressInfo[ad_tel]','$addressInfo[ad_address_xq]','$addressInfo[ad_code]','$o_remark','$value[s_num]',0,0,0,0,'$data[o_type]'),";
                    }
                    $sql = substr($sql,0,-1);
                    Db::query($sql);
                    echo "0";die;
                }else{
                    echo "3";die;
                }
            }else{
                echo "2";die;
            }
        }else{
            echo "1";die;
        }
    }


    // 提交订单成功
    public function truesubmit()
    {
        $u_id = Cookie::get('u_id');
        if (!empty($u_id)) {
            $typeData = $this->alltype();
            return view('truesubmit',['typeData'=>$typeData]);
        }else{
            return redirect("index/index");
        }
        
    }

    // 分类查询
    public function alltype()
    {
        // 查询分类
        $sql = "select * from man_category";
        $typeData = Db::query($sql);
        $typeData = $this->createTreeBySon($typeData);
        return $typeData;
    }
    // 多级分类递归方法  多维数组
    public function createTreeBySon($info,$c_pid=0)
    {
        // 定义数组
        $newArr = array();
        // 处理
        foreach ($info as $k=>$v) {
            // 判断
            if($v['c_pid'] == $c_pid){

                // 把v赋给新数组追加一个下标
                $newArr[$k] = $v;

                // 调用自己 查找所有子辈放到son下
                $newArr[$k]['son'] = $this->createTreeBySon($info,$v['c_id']);
            }
        }
        // 数组返回
        return $newArr;
    }

}
